package com.whoisaa.wifitransfer.base;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.whoisaa.wifitransfer.utils.L;
import com.whoisaa.wifitransfer.utils.NetUtils;
import com.whoisaa.wifitransfer.utils.T;

import java.util.List;

import butterknife.ButterKnife;

/**
 * @Author AA
 * @DateTime 15/12/6 下午10:37
 */
public abstract class BaseFragment extends Fragment {

    protected View mView;

    /**
     * Activity实例
     */
    private FragmentActivity mActivity;
    /**
     * 资源实例
     */
    private Resources mResources;

    /**
     * 获取当前Fragment布局ID
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 获取数据并显示
     */
    public abstract void initData();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = getActivity();
        mResources = mActivity.getResources();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView  = inflater.inflate(getLayoutId(), container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //绑定注解类
        ButterKnife.bind(this, view);
        initData();
    }

    /**
     * 获得上下文
     * @return
     */
    protected FragmentActivity getAppActivity() {
        return mActivity;
    }

    public Resources getAppResources() {
        return mResources;
    }

    /**
     * 获取视图
     * @param resId
     * @return
     */
    protected final View findViewById(int resId) {
        if (resId < 0) {
            return null;
        }
        return mView.findViewById(resId);
    }

    public boolean isEmpty(Object obj) {
        boolean result = false;
        if (obj == null) {
            result = true;
        } else if (obj instanceof TextView) {
            //TextView
            TextView tv = (TextView) obj;
            if (TextUtils.isEmpty(tv.getText())) {
                result = true;
            }
        } else if (obj instanceof EditText) {
            //EditText
            EditText et = (EditText) obj;
            if (TextUtils.isEmpty(et.getText())) {
                result = true;
            }
        } else if (obj instanceof String) {
            //String
            String s = (String) obj;
            if (s.trim().equals("")) {
                result = true;
            }
        } else if (obj instanceof List) {
            List<?> list = (List<?>) obj;
            if (list == null || list.size() <= 0) {
                result = true;
            }
        }
        return result;
    }

    public void writeErrorLogcat(String log) {
        L.error(log);
    }

    public boolean isNetWorkAvailable() {
        return NetUtils.isConnected(getAppActivity());
    }

    public void toast(String msg) {
        T.showShort(getAppActivity(), msg);
    }

    public void toast(int resId) {
        T.showShort(getAppActivity(), resId);
    }

    public void pushActivity(Intent intent) {
        startActivity(intent);
    }

    public void pushActivity(Class<?> mClass) {
        startActivity(new Intent(getAppActivity(), mClass));
    }

    public void pushActivity(Intent intent, boolean finishSelf) {
        pushActivity(intent);

        if (finishSelf)
            mActivity.finish();
    }

    public void pushActivity(Class<?> mClass, boolean finishSelf) {
        pushActivity(mClass);

        if (finishSelf)
            mActivity.finish();
    }

    public void pushActivityForResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
    }

    public void pushActivityForResult(Class<?> mClass, int requestCode) {
        startActivityForResult(new Intent(getAppActivity(), mClass), requestCode);
    }

}
